package cn.hl.hm.logic.st;

import cn.hl.hm.constants.DriverStatus;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.DriverVehicle;
import cn.hl.hm.entity.VehicleLength;
import cn.hl.hm.entity.VehicleType;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.service.DriverVehicleService;
import cn.hl.hm.service.VehicleLengthService;
import cn.hl.hm.service.VehicleTypeService;
import cn.hl.hm.util.List2Map;
import cn.hl.hm.util.http.HttpUtils;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by xianghan on 17/12/13.
 */
@Service
public class StDriverLogic {
    private static final Logger logger = LoggerFactory.getLogger(StDriverLogic.class);
    @Autowired
    private DriverVehicleService driverVehicleService;
    @Autowired
    private DriverService driverService;
    @Autowired
    private VehicleTypeService vehicleTypeService;
    @Autowired
    private VehicleLengthService vehicleLengthService;
    @Resource(name = "configProperties")
    private Properties properties;

//    @Async
    public boolean ayncDriver(Integer driverId) {
        try {
            if (driverId != null) {
                Driver driver = driverService.selectByPrimaryKey(driverId);
                List<DriverVehicle> driverVehicles = driverVehicleService.selectByDIVI(driverId, null);
                if (driver != null && driverVehicles != null) {
                    VehicleType vehicleType = null;
                    VehicleLength vehicleLength = null;
                    for (DriverVehicle driverVehicle : driverVehicles) {
                        vehicleType = vehicleTypeService.selectByPrimaryKey(driverVehicle.getVehicleTypeId());
                        vehicleLength = vehicleLengthService.selectByPrimaryKey(driverVehicle.getVehicleLengthId());
                        if (vehicleType != null && vehicleLength != null) {
                            toAyncDriverVehicel(driverVehicle, driver, vehicleType, vehicleLength);
                            return true;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

//    @Async
    public boolean ayncVehicel(Integer driverVehicleId) {
//        int n = 100 / 0;
        try {
            if (driverVehicleId != null) {
                DriverVehicle driverVehicle = driverVehicleService.selectByPrimaryKey(driverVehicleId);
                if (driverVehicle != null) {
                    Driver driver = driverService.selectByPrimaryKey(driverVehicle.getDriverId());
                    if (driver != null) {
                        VehicleType vehicleType = vehicleTypeService.selectByPrimaryKey(driverVehicle.getVehicleTypeId());
                        VehicleLength vehicleLength = vehicleLengthService.selectByPrimaryKey(driverVehicle.getVehicleLengthId());
                        if (vehicleType != null && vehicleLength != null) {
                            toAyncDriverVehicel(driverVehicle, driver, vehicleType, vehicleLength);
                            return true;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }


    private void toAyncDriverVehicel(DriverVehicle driverVehicle, Driver driver, VehicleType vehicleType, VehicleLength vehicleLength) {
        try {
            StringBuffer info = new StringBuffer();
            info.append("同步司机车辆信息：");
            List<NameValuePair> formParams = new ArrayList<>();
            NameValuePair nameValuePair = null;
//            Map<String, String> postMap = new HashMap<String, String>();
            if (driver != null) {
                nameValuePair = new BasicNameValuePair("userName", driver.getName());
                formParams.add(nameValuePair);
                //电话
                nameValuePair = new BasicNameValuePair("userPhone", driver.getDriverPhone());
                formParams.add(nameValuePair);
                //数据状态 0 有效 1无效
                if (driver.getDriverStatus().equals(DriverStatus.INVAILD.getStatus())
                        || driver.getDriverStatus().equals(DriverStatus.FAIL.getStatus())
                        || driver.getDriverStatus().equals(DriverStatus.WAIT_SUBMIT.getStatus())) {
                    nameValuePair = new BasicNameValuePair("status", "1");
                    formParams.add(nameValuePair);
                } else {
                    nameValuePair = new BasicNameValuePair("status", "0");
                    formParams.add(nameValuePair);
                }
                //司机审核状态
                nameValuePair = new BasicNameValuePair("auditStatus", driver.getDriverStatus());
                formParams.add(nameValuePair);
                //司机工作状态
                nameValuePair = new BasicNameValuePair("workStatus", driver.getWorkStatus());
                formParams.add(nameValuePair);
                nameValuePair = new BasicNameValuePair("driverIdCard", driver.getDriverLicense());
                formParams.add(nameValuePair);
                info.append("司机id：" + driver.getId());
            }
            if (driverVehicle != null) {
                //车辆-司机-车型 唯一id 不可重复
                nameValuePair = new BasicNameValuePair("id", driverVehicle.getId().toString());
                formParams.add(nameValuePair);
                //车牌号
                nameValuePair = new BasicNameValuePair("plateNumber", driverVehicle.getPlateNumber());
                formParams.add(nameValuePair);
                //车辆审核状态
                nameValuePair = new BasicNameValuePair("vehicleStatus", driverVehicle.getStatus().toString());
                formParams.add(nameValuePair);
                //车辆工作状态
                nameValuePair = new BasicNameValuePair("vehicleWorkStatus", driverVehicle.getWorkStatus().toString());
                formParams.add(nameValuePair);
                info.append("车辆id：" + driverVehicle.getId());
            }
            if (vehicleType != null) {
                //车型
                nameValuePair = new BasicNameValuePair("vehicleType", vehicleType.getName());
                formParams.add(nameValuePair);
            }
            if (vehicleLength != null) {
                //车长
                nameValuePair = new BasicNameValuePair("vehicleLength", vehicleLength.getVehicleLength().toString());
                formParams.add(nameValuePair);
            }
            String stUrl = properties.getProperty("st.domain", "") + properties.getProperty("st.driver.sync.uri", "");
            Map<String, String> responseData = HttpUtils.toHttpPost(stUrl, null, formParams, null, null);
            if (responseData != null) {
                info.append("，同步结果:" + responseData.get("responseBody"));
            } else {
                info.append("，同步结果:");
            }
            logger.info(info.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void ayncAllDriverVehicel() {
        try {
            List<Driver> drivers = driverService.selectDriver(null);
            if (drivers != null) {
                List<DriverVehicle> driverVehicles = driverVehicleService.selectByDIVI(null, null);
                if (driverVehicles != null) {
                    Map<Integer, Driver> driverMap = List2Map.getInstance().tobeMap("getId", driverService.selectDriver(null));
                    Map<Integer, VehicleLength> vehicleLengthMap = List2Map.getInstance().tobeMap("getId", vehicleLengthService.selectVehicleLength());
                    Map<Integer, VehicleType> vehicleTypeMap = List2Map.getInstance().tobeMap("getId", vehicleTypeService.selectVehicleType(null));
                    Driver driver = null;
                    VehicleType vehicleType = null;
                    VehicleLength vehicleLength = null;
                    logger.info("开始同步司机车辆到零担系统!数据条数：" + driverVehicles.size());
                    for (int i = 0; i < driverVehicles.size(); i++) {
                        logger.info("当前同步第" + i + "条");
                        driver = driverMap.get(driverVehicles.get(i).getDriverId());
                        vehicleType = vehicleTypeMap.get(driverVehicles.get(i).getVehicleTypeId());
                        vehicleLength = vehicleLengthMap.get(driverVehicles.get(i).getVehicleLengthId());
                        if (driver != null && vehicleType != null && vehicleLength != null) {
                            toAyncDriverVehicel(driverVehicles.get(i), driver, vehicleType, vehicleLength);
                        } else {
                            logger.info("同步车辆" + driverVehicles.get(i).getId() + ",结果:关联数据不完整");
                        }
                    }
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
